// admin/users/users.js
Page({
  data: {
    usersList: [],
    loading: true,
    page: 1,
    hasMore: true
  },

  onLoad: function() {
    this.fetchUsersList();
  },

  fetchUsersList: function() {
    // 模拟获取用户列表数据
    setTimeout(() => {
      const newUsers = [];
      for (let i = 0; i < 10; i++) {
        const id = (this.data.page - 1) * 10 + i + 1;
        newUsers.push({
          id: id,
          username: '用户' + id,
          registerDate: '2023-0' + (Math.floor(Math.random() * 9) + 1) + '-' + (Math.floor(Math.random() * 28) + 1),
          learningDays: Math.floor(Math.random() * 30) + 1,
          totalWords: Math.floor(Math.random() * 1000) + 100
        });
      }

      this.setData({
        usersList: this.data.page === 1 ? newUsers : [...this.data.usersList, ...newUsers],
        loading: false,
        hasMore: this.data.page < 5 // 模拟只有5页数据
      });
    }, 1000);
  },

  // 加载更多
  loadMore: function() {
    if (!this.data.loading && this.data.hasMore) {
      this.setData({
        page: this.data.page + 1,
        loading: true
      });
      this.fetchUsersList();
    }
  },

  // 查看用户详情
  viewUserDetail: function(e) {
    const userId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/admin/users/detail?id=' + userId
    });
  }
});